package com.turbo.cloud.contract.web.webservice;

import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;
import com.turbo.cloud.contract.web.entity.UmsUser;
import com.turbo.cloud.contract.web.service.IUmsUserService;
import com.turbo.cloud.core.api.R;
import com.turbo.cloud.core.api.Result;
import com.turbo.cloud.starter.annotations.unrepeatrequest.UnRepeatRequest;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-08-13
 */
@Slf4j
@RestController
public class UmsUserController {

    @Resource
    private IUmsUserService iUmsUserService;
    @Resource
    private LockTemplate lockTemplate;

    @GetMapping("/webservice/user/getUserList")
    public ResponseEntity<Result<List<UmsUser>>> getUserList() {
        List<UmsUser> list = iUmsUserService.list();

        return ResponseEntity.ok(R.result(list));
    }

    @GetMapping("/interface/user/getUserList2")
    public Result<List<UmsUser>> getUserList2() {
        List<UmsUser> list = iUmsUserService.list();
        return R.result(list);
    }

    @GetMapping("/webservice3/user/getUserList2")
    public Result<List<UmsUser>> getUserList3() {
        List<UmsUser> list = iUmsUserService.list();
        return R.result(list);
    }

    @UnRepeatRequest(timeout = 10)
    @GetMapping("/admin/user/test")
    public Result<String> test() {
        return R.success();
    }

    //@Lock4j(keys = {"#id"}, expire = 10000, acquireTimeout = 1000)
    @GetMapping("/admin/user/test2")
    public Result<String> test2(@RequestParam("id") String id) {
        final LockInfo lockInfo = lockTemplate.lock(id, 30000L, 5000L);
        if (null == lockInfo) {
            throw new RuntimeException("业务处理中,请稍后再试");
        }
        return R.success();
    }
}
